// pages/healthData/index.js
Page({
  data: {
    iotId: '',
    healthData: [],
    loading: false,
    heartRate: '--',
    heartRateTime: '--',
    respiratoryRate: '--',
    respiratoryRateTime: '--',
    sleepState: '--',
    sleepStateTime: '--',
    bedTime: '--',
    bedTimeTime: '--',
    bedExitCount: '--',
    bedExitCountTime: '--'
  },

  onLoad: function (options) {
    // 页面加载时的初始化
    this.setData({
      iotId: options.iotId || ''
    });
    
    if (this.data.iotId) {
      this.loadHealthData();
    }
  },

  // 设备ID输入
  onIotIdInput: function(e) {
    this.setData({
      iotId: e.detail.value
    });
  },

  // 加载健康数据
  loadHealthData: function() {
    if (!this.data.iotId) {
      wx.showToast({
        title: '请输入设备ID',
        icon: 'none'
      });
      return;
    }

    this.setData({
      loading: true
    });

    wx.request({
      url: 'http://localhost:8080/member/user/queryServiceProperties/' + this.data.iotId,
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        console.log('健康数据响应:', res.data);
        
        if (res.data.code === 200 && res.data.data) {
          const healthData = res.data.data;
          this.setData({
            healthData: healthData
          });
          
          // 解析各项健康数据
          this.parseHealthData(healthData);
        } else {
          wx.showToast({
            title: '获取数据失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({
          loading: false
        });
      }
    });
  },

  // 解析健康数据
  parseHealthData: function(propertyStatusInfo) {
    const data = {};
    
    propertyStatusInfo.forEach(item => {
      const identifier = item.identifier;
      const value = item.value;
      const time = this.formatTime(item.time);
      
      switch (identifier) {
        case 'HeartRate':
          data.heartRate = value;
          data.heartRateTime = time;
          break;
        case 'BodyTemp':
          data.bodyTemp = parseFloat(value).toFixed(1);
          data.bodyTempTime = time;
          break;
        case 'xueyang':
          data.xueyang = value;
          data.xueyangTime = time;
          break;
        case 'BatteryPercentage':
          data.battery = parseFloat(value).toFixed(1);
          data.batteryTime = time;
          break;
        case 'xueya':
          data.xueya = value;
          data.xueyaTime = time;
          break;
      }
    });
    
    this.setData(data);
  },

  // 格式化时间
  formatTime: function(timestamp) {
    if (!timestamp) return '--';
    
    const date = new Date(parseInt(timestamp));
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hour = String(date.getHours()).padStart(2, '0');
    const minute = String(date.getMinutes()).padStart(2, '0');
    const second = String(date.getSeconds()).padStart(2, '0');
    
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  },

  // 刷新数据
  refreshData: function() {
    this.loadHealthData();
  },

  // 获取状态类型
  getStatusType: function(identifier) {
    switch (identifier) {
      case 'HeartRate':
        return 'heart';
      case 'BodyTemp':
        return 'temp';
      case 'xueyang':
        return 'oxygen';
      case 'BatteryPercentage':
        return 'battery';
      case 'xueya':
        return 'pressure';
      default:
        return 'normal';
    }
  },

  // 获取状态文本
  getStatusText: function(identifier) {
    switch (identifier) {
      case 'HeartRate':
        return '正常';
      case 'BodyTemp':
        return '正常';
      case 'xueyang':
        return '正常';
      case 'BatteryPercentage':
        return '正常';
      case 'xueya':
        return '正常';
      default:
        return '正常';
    }
  }
}); 